/**
 * Created by Tim on 2017/5/23.
 */
$(function () {
    function RoleMgr() {
        // 表格ID
        this.dataTable = $("#dataTable");
        // 按钮
        this.$seachr_btn = $('#btn_search');
        this.$btn_search_clear = $('#btn_search_clear');

        // 初始化
        this.init();
    }

    RoleMgr.prototype.Constructor = RoleMgr;

    RoleMgr.prototype.init = function () {
        this.loadTable();
        this.registEvent();
    };

    RoleMgr.prototype.loadTable = function () {
        this.dataTable.dataTable({
            "sAjaxSource": "/roles/page",
            "aoColumns": [
                {"sWidth": "16%", "sTitle": "角色名称", "sClass": "center", "mData": "name", "bSortable": false},
                {"sWidth": "10%", "sTitle": "类型", "sClass": "center", "mData": "type", "bSortable": false},
                {"sWidth": "16%", "sTitle": "描述", "sClass": "center", "mData": "description", "bSortable": false},
                {"sWidth": "15%", "sTitle": "创建时间", "sClass": "center", "mData": "created", "bSortable": false},
                {"sWidth": "10%", "sTitle": "状态", "sClass": "center", "mData": "deleted", "bSortable": false},
                {"sWidth": "16%", "sTitle": "操作", "sClass": "center", "mData": "id", "bSortable": false}
            ],
            "aLengthMenu": [10, 30, 50],
            "aaSorting": [],
            "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                var id = aData["id"];

                var type = aData['type'];
                if (type === 1) {
                    $('td:eq(1)', nRow).html('平台');
                } else if (type === 2) {
                    $('td:eq(1)', nRow).html('物业');
                } else if (type === 3) {
                    $('td:eq(1)', nRow).html('商场');
                }

                if (aData['created']) {
                    $('td:eq(3)', nRow).html(SysTool.Date.formatDate(aData['created'], 'yyyy-MM-dd hh:mm:ss'));
                }

                var available = aData['available'];
                if (!available) {
                    $('td:eq(4)', nRow).html('<span class="label label-danger arrowed">已禁用</span>');
                } else {
                    $('td:eq(4)', nRow).html('<span class="label label-success arrowed">已启用</span>');
                }

                // 操作
                var option = '<a title="修改角色信息" href="/roles/' + id + '/edit" class="btn btn-xs btn-info">编辑</a>';
                if (!!available) {
                    option += '&nbsp;<a title="禁用角色" roleId="' + id + '" href="javascript:;" class="btn btn-xs btn-warning">禁用</a>';
                } else {
                    option += '&nbsp;<a title="启用角色" roleId="' + id + '" href="javascript:;" class="btn btn-xs btn-success">启用</a>';
                }
                option += '&nbsp;<a title="删除角色" roleId="' + id + '" href="javascript:;" class="btn btn-xs btn-danger">删除</a>';

                $('td:eq(5)', nRow).html(option);
            },
            "fnServerParams": function (aoData) {
                queryCondition("#search-form", aoData);
            },
            "fnStateLoaded": function (oSettings, oData) {
//                alert(JSON.stringify(oData));
            }
        });

        function queryCondition(queryFormId, aoData) {
            var queryParameters = $(queryFormId).serializeArray();
            $(queryParameters).each(function (i, v) {
                aoData.push(v);
            });
        }
    };

    RoleMgr.prototype.refreshTable = function () {
        this.dataTable.dataTable().fnClearTable();
    };

    RoleMgr.prototype.registEvent = function () {
        var that = this;
        that.$seachr_btn.click(function () {
            var oSettings = that.dataTable.dataTable().fnSettings();
            oSettings._iDisplayStart = 0;
            that.dataTable.dataTable().fnClearTable();
        });

        that.dataTable.on('click', 'a.btn-danger', function () {
            var $that = $(this);
            BootBox.confirm('确定删除当前所选角色？', function (b) {
                if (!b) return;
                var ids = $that.attr('roleId');
                SysTool.Ajax.ajaxJsonCall('roles/' + ids + '/delete', {}, function (data) {
                    that.$seachr_btn.click();
                });
            });
        });

        that.dataTable.on('click', 'a.btn-warning', function () {
            var $that = $(this);
            BootBox.confirm('确定禁用当前角色？', function (b) {
                if (!b) return;
                var ids = $that.attr('roleId');
                SysTool.Ajax.ajaxJsonCall('roles/' + ids + '/enable/', {enable: false}, function (data) {
                    that.$seachr_btn.click();
                });
            });
        });

        that.dataTable.on('click', 'a.btn-success', function () {
            var $that = $(this);
            BootBox.confirm('确定启用当前角色？', function (b) {
                if (!b) return;
                var ids = $that.attr('roleId');
                SysTool.Ajax.ajaxJsonCall('roles/' + ids + '/enable/', {enable: true}, function (data) {
                    that.$seachr_btn.click();
                });
            });
        });

        that.$btn_search_clear.click(function () {
            $('#roleName').val('');
            $('#type').val('');
            that.$seachr_btn.click();
        });

        $('.date-picker').datepicker({autoclose: true}).next().on(ace.click_event, function () {
            //$(this).prev().focus();
        });

        SysTool.Html.bindEnterClick([], that.$seachr_btn);
    };

    window.RoleMgr = new RoleMgr();
});